# -*- coding: utf-8 -*-
from pygame.sprite import Sprite
from . import config
import pygame


class MakePlayer(Sprite):

    def __init__(self, params, controls):
        Sprite.__init__(self)
        self.normal, self.normal_len = params["normal"], len(params["normal"])
        self.down, self.down_len = params["down"], len(params["down"])
        #\K\yumruk
        self.run, self.run_len = params["run"], len(params["run"])
        self.hand_hit = params["hand_hit"]
        self.hand_h_len = len(params["hand_hit"])
        self.foot_hit = params["foot_hit"]
        self.foot_h_len = len(params["foot_hit"])
        self.jump, self.jump_len = params["jump"], len(params["jump"])
        self.image = self.normal[0]
        self.image.set_colorkey((255, 255, 255))
        self.say = 0
        self.rect = self.image.get_rect()
        self.saldiri1 = 5
        #Ataklar
        self.attk_pow = params["power"]
        self.Atak = False
        self.can = config.initial_life
        self.ynr = True
        #attrs
        self.name = params["name"]
        self.gravity = config.gravity
        self.velocity = params["velocity"]
        self.h = self.image.get_rect().height
        self.w = self.image.get_rect().width
        self.controls = controls
        self.jump_limit = config.y - (config.y / 2)
        if controls == "LEFT":
            self.CLEFT = True
            self.rect.x = 50
            self.rect.y = config.y - self.image.get_rect().height
            self.initial_y = self.rect.y
            self.profil = params["profile"]
        else:
            self.CLEFT = False
            self.rect.x = config.x - self.image.get_rect().height - 50
            self.rect.y = config.y - self.image.get_rect().height
            self.initial_y = self.rect.y
            self.profil = pygame.transform.flip(
                    params["profile"],
                    1,
                    0
                )
        #flags
        self.nwins = 0
        self.KO = False
        self.win = False
        self.lose = False
        self.x_mov = True
        self.in_def = False
        self.under_atk = False
        self.jump_top = False

    def check_win(self):
        if self.win:
            self.nwins += 1

    def reset(self):
        self.can = config.initial_life
        self.KO = False
        self.win = False
        self.lose = False
        self.x_mov = True
        self.in_def = False
        self.under_atk = False
        self.jump_top = False

        if self.CLEFT:
            self.image = self.normal[0]
            self.rect.x = 50
        else:
            self.rect.x = config.x - self.image.get_rect().height - 50
            self.image = pygame.transform.flip(
                    self.normal[0],
                    1,
                    0
                )
        self.rect.y = self.initial_y

    def check_events(self):
        if self.controls == "LEFT":
            UP = pygame.key.get_pressed()[pygame.K_w]
            DOWN = pygame.key.get_pressed()[pygame.K_s]
            RIGHT = pygame.key.get_pressed()[pygame.K_d]
            LEFT = pygame.key.get_pressed()[pygame.K_a]
            B = pygame.key.get_pressed()[pygame.K_g]
            A = pygame.key.get_pressed()[pygame.K_f]
        else:
            UP = pygame.key.get_pressed()[pygame.K_UP]
            DOWN = pygame.key.get_pressed()[pygame.K_DOWN]
            RIGHT = pygame.key.get_pressed()[pygame.K_RIGHT]
            LEFT = pygame.key.get_pressed()[pygame.K_LEFT]
            B = pygame.key.get_pressed()[pygame.K_k]
            A = pygame.key.get_pressed()[pygame.K_j]

        return UP, DOWN, RIGHT, LEFT, A, B

    def pos(self):
        return self.rect.x, self.rect.y

    def x(self):
        return self.rect.x

    def y(self):
        return self.rect.y

    def eksican(self, sayi):
        self.can = self.can - sayi

    def update(self):

        UP, DOWN, RIGHT, LEFT, A, B = self.check_events()

        jc = bool(self.jump_top or (not UP and self.rect.y < self.initial_y))

        if jc:
            if not self.rect.y == self.initial_y:
                self.rect.y = self.rect.y + self.gravity
                self.image = self.jump[0]
            else:
                self.jump_top = False

        if not DOWN and self.in_def:
            self.in_def = False

        if (self.ynr and self.CLEFT) or (not self.ynr and not self.CLEFT):

            UP_condt = bool(UP and not self.under_atk and not self.jump_top)

            if not self.KO and UP_condt:
                if self.say >= self.jump_len:
                    self.say = 0
                self.image = self.jump[0]
                self.image.set_colorkey((255, 255, 255))
                if self.y() <= self.jump_limit:
                    self.jump_top = True
                self.rect.y = self.rect.y - self.gravity

            if not self.KO and RIGHT and not self.under_atk:
                    if self.say >= self.run_len:
                        self.say = 0
                    if not UP:
                        self.image = self.run[self.say]
                    else:
                        self.image = self.jump[0]
                    self.image.set_colorkey((255, 255, 255))
                    if self.x() <= (config.x - self.w) and self.x_mov:
                        self.rect.x = self.rect.x + self.velocity

                    self.say += 1

            elif not self.KO and DOWN and not self.under_atk:
                if self.say >= self.normal_len:
                    self.say = 0
                self.image = self.down[self.say]
                self.image.set_colorkey((255, 255, 255))

                self.say += 1

                self.in_def = True

            elif not self.KO and B and not self.under_atk:
                    if self.say >= self.foot_h_len:
                        self.say = 0

                    elif self.say == 2:
                        self.saldiri1 = self.attk_pow["foot_hit"]
                        self.Atak = True

                    else:
                        self.Atak = False

                    self.image = self.foot_hit[self.say]
                    self.image.set_colorkey((255, 255, 255))
                    self.say += 1

            elif not self.KO and LEFT and not self.under_atk:
                    if self.say >= self.run_len:
                        self.say = 0

                    if not UP:
                        image = self.run[self.say]
                    else:
                        image = self.jump[0]

                    self.image = pygame.transform.flip(
                        image,
                        1,
                        0
                    )
                    self.image.set_colorkey((255, 255, 255))
                    if self.x() >= 0:
                        self.rect.x = self.rect.x - self.velocity
                    self.say += 1

            elif not self.KO and A and not self.under_atk:

                    if self.say >= self.hand_h_len:
                        self.say = 0

                    elif self.say == 2:
                        self.saldiri1 = self.attk_pow["hand_hit"]
                        self.Atak = True

                    else:
                        self.Atak = False

                    self.image = self.hand_hit[self.say]
                    self.image.set_colorkey((255, 255, 255))
                    self.say += 1

            elif not self.KO and not self.under_atk and not jc and not UP:
                if self.say >= self.normal_len:
                    self.say = 0

                self.image = self.normal[self.say]
                self.Atak = False

        if (not self.ynr and self.CLEFT) or (self.ynr and not self.CLEFT):

            if not self.KO and UP and not self.under_atk and not self.jump_top:
                if self.say >= self.jump_len:
                    self.say = 0
                self.image = self.jump[0]
                self.image.set_colorkey((255, 255, 255))
                if self.y() <= self.jump_limit:
                    self.jump_top = True
                self.rect.y = self.rect.y - self.gravity

            if not self.KO and RIGHT and not self.under_atk:
                    if self.say >= self.run_len:
                        self.say = 0

                    if not UP:
                        self.image = self.run[self.say]
                    else:
                        self.image = self.jump[0]

                    self.image.set_colorkey((255, 255, 255))

                    if self.x() <= (config.x - self.w):
                        self.rect.x = self.rect.x + self.velocity

                    self.say += 1

            elif not self.KO and DOWN and not self.under_atk:
                if self.say >= self.normal_len:
                    self.say = 0
                self.image = pygame.transform.flip(
                    self.down[self.say],
                    1,
                    0
                )
                self.image.set_colorkey((255, 255, 255))

                self.say += 1

                self.in_def = True

            elif not self.KO and B and not self.under_atk:
                    if self.say >= self.foot_h_len:
                        self.say = 0

                    elif self.say == 2:
                        self.saldiri1 = self.attk_pow["foot_hit"]
                        self.Atak = True

                    else:
                        self.Atak = False

                    self.image = pygame.transform.flip(
                        self.foot_hit[self.say],
                        1,
                        0
                    )
                    self.image.set_colorkey((255, 255, 255))
                    self.say += 1

            elif not self.KO and LEFT and not self.under_atk:
                    if self.say >= self.run_len:
                        self.say = 0

                    if not UP:
                        image = self.run[self.say]
                    else:
                        image = self.jump[0]

                    self.image = pygame.transform.flip(
                        image,
                        1,
                        0
                    )
                    self.image.set_colorkey((255, 255, 255))
                    if self.x() >= 0 and self.x_mov:
                        self.rect.x = self.rect.x - self.velocity
                    self.say += 1

            elif not self.KO and A and not self.under_atk:
                    if self.say >= self.hand_h_len:
                        self.say = 0
                    elif self.say == 2:
                        self.saldiri1 = self.attk_pow["hand_hit"]
                        self.Atak = True
                    else:
                        self.Atak = False

                    self.image = pygame.transform.flip(
                        self.hand_hit[self.say],
                        1,
                        0
                    )
                    self.image.set_colorkey((255, 255, 255))
                    self.say += 1

            elif not self.KO and not self.under_atk and not jc and not UP:
                if self.say >= self.normal_len:
                    self.say = 0
                self.image = pygame.transform.flip(
                    self.normal[self.say],
                    1,
                    0
                )
                self.Atak = False
